
import http, { baseHeaders, baseParams } from "@/utils/request";
import { UrlChangePassword, UrlLogin } from "@/utils/apiUrl";
import { Toast } from "antd-mobile";
import { isEmptyString } from "@/utils/utils";
import ZjcUtil from "@/utils/zjcUtil";


/**
 * 修改密码
 * @param {string} oldPassword 旧密码
 * @param {string} newPassword 新密码
 * @returns thunk
 */
export const chagePassword = (oldPassword, newPassword, reqCallback) => {
    return async dispatch => {
        console.log("chagePassword: " + UrlChangePassword);

        //组装参数
        const params = baseParams()
        params.oldPassword = oldPassword + ""
        params.newPassword = newPassword + ""
        params.zjc = ZjcUtil.zjc || ""

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlChangePassword, params, headers)
        console.log(res);
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("修改密码回调-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}
